package com.atguigu.gmall.product.rpc;

import com.atguigu.gmall.common.result.Result;
import com.atguigu.gmall.item.to.CategoryView;
import com.atguigu.gmall.product.entity.SkuImage;
import com.atguigu.gmall.product.entity.SkuInfo;
import com.atguigu.gmall.product.entity.SpuSaleAttr;
import com.atguigu.gmall.product.service.BaseCategory1Service;
import com.atguigu.gmall.product.service.SkuImageService;
import com.atguigu.gmall.product.service.SkuInfoService;
import com.atguigu.gmall.product.service.SpuSaleAttrService;
import com.atguigu.gmall.product.to.ValueSkuJson;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.util.List;

/**
 * @Author:lyd
 * @Date:2023/8/18 20:56
 * @Version:1.0
 * @Description
 **/
@RestController
@RequestMapping("/api/inner/product")
public class SkuDetailItemRpcController {

    @Autowired
    private BaseCategory1Service baseCategory1Service;

    @Autowired
    private SkuInfoService skuInfoService;

    @Autowired
    private SkuImageService skuImageService;

    @Autowired
    private SpuSaleAttrService spuSaleAttrService;

    /**
     * 查询三级分类层级路径
     * @param c3Id
     * @return
     */
    @GetMapping("/categoryview/{c3Id}")
    public Result<CategoryView> getCategoryView(@PathVariable("c3Id") Long c3Id) {
        CategoryView categoryView = baseCategory1Service.getCategoryView(c3Id);
        return Result.ok(categoryView);
    }


    /**
     * 查询商品基本信息
     * @param skuId
     * @return
     */
    @GetMapping("/skuinfo/{skuId}")
    public Result<SkuInfo> getSkuinfo(@PathVariable("skuId") Long skuId) {
        SkuInfo skuInfo = skuInfoService.getById(skuId);
        return Result.ok(skuInfo);
    }

    /**
     * 查询商品价格
     * @param skuId
     * @return
     */
    @GetMapping("/price/{skuId}")
    public Result<BigDecimal> getPrice(@PathVariable("skuId") Long skuId) {
        SkuInfo price = skuInfoService.lambdaQuery()
                .select(SkuInfo::getPrice)
                .eq(SkuInfo::getId, skuId)
                .one();
        return Result.ok(price.getPrice());
    }

    /**
     * 查询商品图片
     * @param skuId
     * @return
     */
    @GetMapping("/images/{skuId}")
    public Result<List<SkuImage>> getImages(@PathVariable("skuId") Long skuId){
        List<SkuImage> list = skuImageService.lambdaQuery()
                .eq(SkuImage::getSkuId, skuId)
                .list();
        return Result.ok(list);
    }


    /**
     * 查询当前sku对应的spu定义的所有销售属性名和值，以及选中当前spu对应的哪一种组合
     *
     * @param spuId
     * @param skuId
     * @return
     */
    @GetMapping("/spusaleattr/{spuId}/{skuId}")
    public Result<List<SpuSaleAttr>> getSpuSaleAttr(@PathVariable("spuId") Long spuId,
                                                    @PathVariable("skuId") Long skuId) {
        List<SpuSaleAttr> list = spuSaleAttrService.getSpuSaleAttr(spuId, skuId);
        return Result.ok(list);
    }

    /**
     * 查询当前spuId下分配的各种sku的销售属性id组合
     * @param spuId
     * @return
     */
    @GetMapping("/valuesskuJson/{spuId}")
    Result<String> getValueSkuJson(@PathVariable("spuId") Long spuId){
        String json = spuSaleAttrService.getValueSkuJson(spuId);
        return Result.ok(json);
    }
}
